default_platform(:ios)

platform :ios do
  #更新代码	
  desc "pull the latest code from code server"
  private_lane :pull_latest do
	puts "******获取当前目录******"
	currentPath = sh("pwd")
	puts "******获取工程当前分支******"
	currentBranchName = git_branch
	puts "******当前工程分支为" + currentBranchName + "******"

	puts "******更新相应分支代码******"
  	changeBranchSH = "bash branch.sh " + currentBranchName + " " + currentPath
  	sh changeBranchSH
  end

  #构建应用
  desc "Build Charles"
  private_lane :BuildCharles do |options|
	
	#不生成README
  	skip_docs
	
	#拉取最新代码，并切换到指定分支
	pull_latest()
	
	#更新cocoapods
  	cocoapods(
    		clean:true,
		use_bundle_exec: false,
		try_repo_update_on_error:true	
  	)

	#build号自增
	buildNumber = options[:buildNo]
	increment_build_number(build_number:buildNumber)
	
	#构建打包
	configuration = options[:configuration]
	method = options[:method]
	dateStr = Time.new.strftime("%Y-%m-%d-%H_%M_%S")
  	output_name = "Charles"+ dateStr +".ipa"
  	output_root_directory = "../CharlesBuild/" + dateStr + "/"
  	output_build_path = output_root_directory + "build"
  	output_log_path = output_root_directory + "log"
  	output_archives_path = output_root_directory + "archives"
	gym(
		scheme: "Charles",
		output_name: output_name,
		output_directory: output_build_path,
		buildlog_path: output_log_path,
		build_path: output_archives_path,
		clean: true,
		include_bitcode: false,
		export_method: method,
		export_xcargs: "-allowProvisioningUpdates",
		configuration: configuration
   	 )
  	end
	
	#构建Debug包
	desc "Build Charles Debug"
	lane :CharlesDebug do |options|
		buildNumber = options[:buildNo]
		BuildCharles(
			configuration: "Debug",
			method: "development",
			buildNo: buildNumber
		)
	#上传到fir
	#firim(firim_api_token:"xxx")
	end

	#构建Release包
	desc "Build Charles Release"
	lane :CharlesRelease do |options|
		buildNumber = options[:buildNo]
		BuildCharles(
			configuration: "AdHoc",
			method: "ad-hoc",
			buildNo: buildNumber
		)
	#上传到fir
	#firim(firim_api_token:"xxx")
	end
	
	#根据传入的参数来构建不同环境的包
	desc "Build Charles With Parameters"
	lane :Charles do |options|
		buildNumber = options[:buildNo]
		buildTypeStr = options[:buildType]
		if buildTypeStr == 'Debug'
			CharlesDebug(buildNo:buildNumber)
		else
			CharlesRelease(buildNo:buildNumber)
	end
  end
end
